<?php defined('SYSPATH') or die('No direct script access.');

class Helper_Admin
{
    public static function menu($current = 'review')
    {
        $menu = array(
            'Основное' => array(
                'products/reviews' => 'Товары и отзывы',
                'widgets/reviews' => 'Виджеты',
            ),
            'Настройки' => array(
                'index/shop' => 'Магазин',
                'index/profile' => 'Профиль',
            ),
            'Полезное' => array(
                'index/start' => 'Быстрый старт',
            ),
        );
        
        $content = new View('admin/_menu_tpl');
        $content->menu = $menu;
        $content->current = $current;
        return $content;
    }
    
    public static function tickets_sidebar(Model_Mailbox $mailbox, $current = null)
    {
        $filters = Model_Ticket::get_available_filters();
        $filter_cout = $mailbox->get_mailbox_stat();
        
        foreach ($filters as $url => &$filter) {
            $filter['count'] = $filter_cout[$url];    
        }
        
        $content = new View('admin/ticket/_tickets_sidebar_tpl');
        $content->mailbox = $mailbox;
        $content->filters = $filters;
        $content->current = $current;
        return $content;
    }
    
    public static function tickets_list($tickets, $count, $count_active, $filter = null, $all_users = array())
    {
        $content = new View('admin/ticket/_tickets_list_tpl');
        $content->tickets = $tickets;
        $content->count   = $count;
        $content->count_active = $count_active;
        $content->filter  = $filter;
        $content->all_users = $all_users;
        return $content;      
    }
    
    public static function add_mailbox_url()
    {
        return '/mailbox/add';
    }
    
    public static function mailbox_url(Model_Mailbox $mailbox, $filter = null)
    {
        $url = '/mailbox/'.$mailbox->hash;
        
        $filter_url = Model_Ticket::FILTER_UNASSIGNED;
        if ($filter) {
            $filter_url = $filter['url'];
        }
        $url .= '/'.$filter_url;    
        return $url; 
    }
    
    public static function forward_mailbox_url(Model_Mailbox $mailbox)
    {
        return '/mailbox/forward/'.$mailbox->hash;   
    }
    
    public static function save_reply_mailbox_url(Model_Mailbox $mailbox)
    {
        return '/mailbox/save-reply/'.$mailbox->hash;   
    }
    
    public static function mailbox_delete_url(Model_Mailbox $mailbox)
    {
        return '/mailbox/delete/'.$mailbox->hash;   
    }
    
    public static function add_ticket_url(Model_Mailbox $mailbox)
    {
        return '/ticket/add/'.$mailbox->hash;
    }
    
    public static function assign_ticket_url(Model_Ticket $ticket, $assigned)
    {
        $assigned_id = $assigned;
        if ($assigned instanceof Model_User) {
            $assigned_id = $assigned->id;    
        }
        return '/ticket/assign/'.$ticket->id.'/'.$assigned_id;
    }
    
    public static function status_ticket_url(Model_Ticket $ticket, $status)
    {
        return '/ticket/status/'.$ticket->id.'/'.$status;
    }
    
    public static function delete_ticket_url(Model_Ticket $ticket)
    {
        return '/ticket/delete/'.$ticket->id;  
    }
    
    public static function ticket_url(Model_Ticket $ticket)
    {
        return '/ticket/'.$ticket->id;
    }
    
    public static function user_url()
    {
        return '/user';
    }
    
    public static function user_profile_url($user = null)
    {
        if ($user) {
            return '/user/profile/'.$user->id;   
        }
        return '/user/profile';
    }
    
    public static function user_delete_url(Model_User $user)
    {
        return '/user/delete/'.$user->id;   
    }
    
    public static function user_invite_url(Model_User $user, $is_absolute = false)
    {
        $url = '';
        if ($is_absolute) {
            $company = ORM::factory('company')->get_current();
            $url = 'http://'.$company->domain.'.teamdesk.ru';    
        }
        
        return $url.'/user-invite/'.$user->invite;   
    }
    
    public static function pagination($page, $per_page, $count, $url, $first_url, $sort = null, $params = null)
    {
        if (ceil($count / $per_page) <= 1) {
            return '';
        }
        $content = new View('admin/_pagination_tpl');
        $content->page     = $page;
        $content->per_page = $per_page;
        $content->count    = $count;
        $content->url      = $url;
        $content->first_url = $first_url;
        $content->sort     = $sort;
        if ($params) {
            $empty = true;
            foreach ($params as &$param) {
                if (is_null($param)) {
                    $param = '';    
                } else if (is_array($param) && !count($param)) {
                    $param = '';    
                } else {
                    $empty = false;    
                }   
            }
            if ($empty) {
                $params = null;    
            }
        }
        $content->params   = $params;
        return $content;   
    }
    
    /**
    *  Множественное число
    *
    * @param int $howmuch
    * @param array $input Например: [0] - комментарий, [1] - комментария, [2] - комментариев
    * @return string
    */
    public static function plural_form($howmuch, array $input) {
        $howmuch = (int)$howmuch;
        $l2 = substr($howmuch,-2);
        $l1 = substr($howmuch,-1);
        if($l2 > 10 && $l2 < 20) return $input[2];
        else
        switch ($l1) {
            case 0: return $input[2]; break;
            case 1: return $input[0]; break;
            case 2: case 3: case 4: return $input[1]; break;
            default: return $input[2]; break;
        }
        return false;
    }
    
    public static function prepare_text($text)
    {
        $text = preg_replace('@<base[^>]*?>@si', '', $text);
        
        return $text;    
    }
    
    public static function make_links_blank($text)
    {
        return $text;
        return  preg_replace(
            array(
                '/(?(?=<a[^>]*>.+<\/a>)
                     (?:<a[^>]*>.+<\/a>)
                     |
                     ([^="\']?)((?:https?|ftp|bf2|):\/\/[^<> \n\r]+)
                 )/iex',
                '/<a([^>]*)target="?[^"\']+"?/i',
                '/<a([^>]+)>/i',
                '/(^|\s)(www.[^<> \n\r]+)/iex',
                '/(([_A-Za-z0-9-]+)(\\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-]+)
                (\\.[A-Za-z0-9-]+)*)/iex'
            ),
            array(
                "stripslashes((strlen('\\2')>0?'\\1<a href=\"\\2\">\\2</a>\\3':'\\0'))",
                '<a\\1',
                '<a\\1 target="_blank">',
                "stripslashes((strlen('\\2')>0?'\\1<a href=\"http://\\2\">\\2</a>\\3':'\\0'))",
                "stripslashes((strlen('\\2')>0?'<a href=\"mailto:\\0\">\\0</a>':'\\0'))"
            ),
            $text
        );
    }
    
    public static function delete_links($text)
    {
        return $text;
        return strip_tags($text, '<p><div><small><i><b><u><strong><img><span><blockquote><br>');
        /*return preg_replace('#<a.*?>(.*?)</a>#i', '\1', $text);   */
    }
    
    public static function strip_tags_content($text, $tags = '', $invert = FALSE)
    { 
        preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags); 
        $tags = array_unique($tags[1]); 

        if (is_array($tags) AND count($tags) > 0) { 
            if($invert == FALSE) { 
                return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text);
            } 
            else { 
                return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text); 
            } 
        } elseif ($invert == FALSE) { 
            return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text); 
        } 
        return $text; 
    } 
    
    public static function strip_selected_tags($text, $tags = array())
    {
        $args = func_get_args();
        $text = array_shift($args);
        $tags = func_num_args() > 2 ? array_diff($args,array($text))  : (array)$tags;
        foreach ($tags as $tag) {
            if(preg_match_all('/<'.$tag.'[^>]*>(.*)<\/'.$tag.'>/iU', $text, $found)){
                $text = str_replace($found[0],$found[1],$text);
            }
        }

        return $text;
    }
} 
